# -*- coding:UTF-8 -*-
from selenium import webdriver
import time


def selenium_init(url):
    # webdriver 驱动路径
    path = 'E:/desktop/chromedriver_win32/chromedriver.exe'
    driver = webdriver.Chrome(executable_path=path)
    driver.get(url)
    return driver


def format_time(str_time):
    array = time.strptime(str_time, u"%Y年%m月%d日")
    try:
         new_time = time.strftime("%Y-%m-%d", array)
         return new_time
    except Exception as e:
        print(e)

def get_figure_info(figure_names):
    url = 'https://baike.sogou.com/'
    driver = selenium_init(url=url)
    figures = []
    for figure_name in figure_names:
        figure = {}
        input = driver.find_element_by_id('searchText')
        input.click()
        input.clear()
        input.send_keys(figure_name)
        search = driver.find_element_by_id('enterLemma')
        search.click()
        title = driver.find_elements_by_css_selector('#baseInfoCol > table > tbody > tr > td:nth-child(1) > table > tbody')
        try:
            # 姓名
            figure['figure_name' ]= figure_name

            # 出生日期
            birth_time = driver.find_element_by_css_selector('#baseInfoCol > table > tbody > tr > td:nth-child(1) > table > tbody > tr:nth-child(2) > td > div > div').text
            birth_time = format_time(birth_time)
            figure['birth_time'] = birth_time

            # 逝世日期
            death_time = driver.find_element_by_css_selector('#baseInfoCol > table > tbody > tr > td:nth-child(2) > table > tbody > tr:nth-child(2) > td > div > div').text
            death_time = format_time(death_time)
            figure['death_time'] = death_time

            # 年龄
            figure['age'] = time.strptime(death_time,'%Y-%m-%d').tm_year - time.strptime(birth_time,'%Y-%m-%d').tm_year

            # 出生地
            birth_place = driver.find_element_by_css_selector('#baseInfoCol > table > tbody > tr > td:nth-child(2) > table > tbody > tr:nth-child(1) > td > div > div').text
            figure['birth_place'] = birth_place

            avatar = driver.find_element_by_css_selector('#lemma_pic > a > img')
            avatar_src = avatar.get_attribute('src')
            # 头像
            figure['avatar_src'] = avatar_src

            summary = driver.find_element_by_css_selector('#main > div.main_wrap > div.lemma_container > div.abstract_wrap > div.abstract_main > div.abstract > p').text
            # 人物总结
            figure['figure_summary'] = summary
            # experiences = driver.find_elements_by_css_selector("body > div.body-wrapper > div.content-wrapper > div > div.main-content  div.lemma-summary ~ div.para")
            # expe = []
            # for item in experiences:
            #     expe.append(item.text)
            # figure['location'] = expe
            figures.append(figure)
            driver.back()
        except Exception  as e:
            print(e)
            driver.back()
            continue
    return figures


names = ['释虚云', '齐白石', '徐右丞', '葛健豪', '贺民范', '谢国梁', '杜心五', '杨昌济', '郑家溉', '龙廷久']
str = get_figure_info(names)
print(str)